package com.bi.onlineEducation.utils;

import com.bi.onlineEducation.exception.BusinessHandleCodeEnum;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.Serializable;

/**
 * @ClassName ResultCode
 * @Author Administrator
 * @Date 2021/1/8 14:01
 * @Version 1.0
 */
public class ResultCode<T> implements Serializable {

    private static final Logger logger = LogManager.getLogger(ResultCode.class);

    private static final long serialVersionUID = 1L;
    /**
     * 错误码
     */
    private Integer code = 0;

    /**
     * 返回详细信息
     */
    private String message = null;

    /**
     * 返回状态标志
     */
    private Boolean flag = false;

    /**
     * 返回结果实体
     */
    private T data = null;


    public ResultCode() {
    }

    public ResultCode(Integer code, Boolean flag, String message, T response) {
        this.code = code;
        this.flag = flag;
        this.message = message;
        this.data = response;
    }

    public ResultCode(Integer code, Boolean flag, String message) {
        this.code = code;
        this.flag = flag;
        this.message = message;
    }

    public ResultCode(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getDesc();
    }

    public ResultCode(ResultEnum resultEnum, T response) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getDesc();
        this.data = response;
    }


    public static <T> ResultCode<T> error(String message) {
        logger.debug("返回错误：code={}, message={}", ResultEnum.ERROR.getCode(), message);
        return new ResultCode<T>(ResultEnum.ERROR.getCode(), false, message);
    }

    public static <T> ResultCode<T> error(BusinessHandleCodeEnum businessHandleCodeEnum) {
        logger.debug("返回错误：code={}, message={}", businessHandleCodeEnum.getCode(), businessHandleCodeEnum.getMsg());
        return new ResultCode<T>(businessHandleCodeEnum.getCode(), false, businessHandleCodeEnum.getMsg());
    }

    public static <T> ResultCode<T> error(int code, String message) {
        logger.debug("返回错误：code={}, message={}", code, message);
        return new ResultCode<T>(code, false, message);
    }

    public static <T> ResultCode<T> success(T response) {
        return new ResultCode<T>(BusinessHandleCodeEnum.SUCCESS.getCode(), true, BusinessHandleCodeEnum.SUCCESS.getMsg(), response);
    }

    public static Logger getLogger() {
        return logger;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Boolean getFlag() {
        return flag;
    }

    public void setFlag(Boolean flag) {
        this.flag = flag;
    }

    public T getData() {
        return data;
    }

    public void setData(T response) {
        this.data = response;
    }
}

